﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtyManagementSystem.DAL;

namespace RealtyManagementSystem.Web.RealtyManagers
{
    public partial class PublishAnnocement : System.Web.UI.Page
    {
        protected Guid AuthorId
        {
            get
            {
                object strId = this.ViewState["authorId"];
                if (strId != null)
                    return new Guid(strId.ToString());
                return new Guid();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAuthor();
                BindDate();
            }
        }

        private void BindDate()
        {
            this.lblCreateDate.Text = DateTime.Now.ToLongDateString() + " \t"
                + DateTime.Now.ToShortTimeString();
        }

        public void BindAuthor()
        {
            var user = Membership.GetUser();
            this.lblAuthor.Text = user.UserName;
            this.ViewState["authorId"] = user.ProviderUserKey.ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var announce = new BLL.AnnouncementsProvider();
            var ann = announce.Add(new Announcement()
             {
                 AuthorID = this.AuthorId,
                 Title = this.txtTitle.Text,
                 Content = this.txtContent.Text,
                 CreateDate = DateTime.Now
             });

            Response.Redirect("~\\Common\\AnnouncementPreview.aspx?announceId=" + ann.AnnounceId);
        }
    }//class
}